package com.ucap.weking.guangan.fragments.publicFragment;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.base.BaseFragment;
import com.ucap.weking.guangan.views.MyToast;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/10/26.
 */

public class WebFragment extends BaseFragment {
    @InjectView(R.id.wb_content)
    WebView wbContent;

    @InjectView(R.id.pb_loading)
    ProgressBar pbLoading;
    String uri;

    public WebFragment setUri(String uri) {
        this.uri = uri;
        Log.i("WebFragment", "setUri: " + uri);
        return this;
    }


    @Override
    public void intiView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.web_fragment, null);
        flContent.addView(view);
        ButterKnife.inject(this, view);
    }

    @Override
    public void setData() {
        setWebViewContent();
    }

    private void setWebViewContent() {
        WebSettings webSettings = wbContent.getSettings();
        wbContent.setInitialScale(100);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染速度
        webSettings.setUseWideViewPort(true);//关键点
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        webSettings.setCacheMode(WebSettings.);//缓存模式，（不使用本地缓存）
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setLoadWithOverviewMode(true);
        wbContent.setVisibility(View.INVISIBLE);
        wbContent.loadUrl(uri);
        wbContent.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                isErr = true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!isErr) {
                } else {
                }

            }
        });
        wbContent.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pbLoading.setVisibility(View.GONE);
                    wbContent.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i("errorTitle", "onReceivedTitle: " + title);
                if (!TextUtils.isEmpty(title) && title.toLowerCase().contains("找不到网页")) {
                    isErr = true;
                }
            }
        });

    }

    boolean isErr = false;

}
